package com.hsmw.api.service;


import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.hsmw.api.mapper.HsmwPointsDetailsMapper;
import com.hsmw.api.mapper.HsmwReportMapper;
import com.hsmw.api.mapper.HsmwUserDriverMapper;
import com.hsmw.common.base.BaseService;
import com.hsmw.domain.HsmwOrder;
import com.hsmw.domain.HsmwPointsDetails;
import com.hsmw.domain.HsmwReport;
import com.hsmw.domain.HsmwUserDriver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public class HsmwPointsDetailsService extends BaseService<HsmwPointsDetailsMapper, HsmwPointsDetails> {



    @Autowired
    private HsmwPointsDetailsMapper hsmwPointsDetailsMapper;

    @Autowired
    private HsmwUserDriverMapper hsmwUserDriverMapper;


    public Map findPointsDetailList(String driverId) {

        Map map = new HashMap();
        List<HsmwPointsDetails> list = new ArrayList<>();
        List<HsmwPointsDetails> hsmwPointsDetails = hsmwPointsDetailsMapper.selectList(new LambdaQueryWrapper<HsmwPointsDetails>().eq(HsmwPointsDetails::getDriverId, driverId).orderByDesc(HsmwPointsDetails::getCreateTime));
        for (HsmwPointsDetails hsmwPointsDetail : hsmwPointsDetails) {
            if("购物消费".equals(hsmwPointsDetail.getType())){
                list.add(hsmwPointsDetail);
            }
        }
        if(CollectionUtil.isNotEmpty(list)){
            BigDecimal sumUsedIntegral = list.stream().map(HsmwPointsDetails::getTypeIntegral).reduce(BigDecimal.ZERO, BigDecimal::add);
            map.put("sumUsedIntegral",sumUsedIntegral);
        }

        HsmwUserDriver hsmwUserDriver = hsmwUserDriverMapper.selectById(driverId);
        String driverIntegral = hsmwUserDriver.getDriverIntegral();
        map.put("HsmwPointsDetails",hsmwPointsDetails);
        map.put("driverIntegral",driverIntegral);
        return map;
    }
}
